<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户管理</title>
<jsp:include page="../inc.jsp"></jsp:include>

<script type="text/javascript">
//构建右键菜单的插件，代码如下：
Ext.ux.TabCloseMenu = function(){
     var tabs, menu, ctxItem;
     this.init = function(tp){
         tabs = tp;
         tabs.on('itemcontextmenu', onContextMenu);
     }

     function onContextMenu(ts, item, e){
         if(!menu){ // create context menu on first right click
             menu = new Ext.menu.Menu([{
                 id: tabs.id + '-close',
                 text: '关闭标签',
                 iconCls : 'btnno',
                 handler : function(){
                     tabs.remove(ctxItem);
                 }
             },{
                 id: tabs.id + '-close-others',
                 text: '关闭其他标签',
                 iconCls : 'btnno',
                 handler : function(){
                     tabs.items.each(function(item){
                         if(item.closable && item != ctxItem){
                             tabs.remove(item);
                         }
                     });
                 }
             },{
                 id: tabs.id + '-close-all',
                 text: '关闭所有标签',
                 iconCls : 'btnno',
                 handler : function(){
                     tabs.items.each(function(item){
                         if(item.closable){
                            tabs.remove(item);
                        }
                     });
                 }
             }]);
         }
         ctxItem = item;
         var items = menu.items;
         items.get(tabs.id + '-close').setDisabled(!item.closable);
         var disableOthers = true;
         tabs.items.each(function(){
             if(this != item && this.closable){
                 disableOthers = false;
                 return false;
             }
         });
         items.get(tabs.id + '-close-others').setDisabled(disableOthers);
         menu.showAt(e.getPoint());
         
         
         var disableAll = true;
          tabs.items.each(function(){
              if(this.closable){
                  disableAll = false;
                  return false;
              }
         });
         items.get(tabs.id + '-close-all').setDisabled(disableAll);
          menu.showAt(e.getPoint());
     }
};
	Ext.onReady(function() {
		var leftPanel = Ext.create('Ext.panel.Panel', {
			id : 'leftPanel',
			region : 'west',
			title : '导航栏',
			width : 200,
			minSize : 200,
			maxSize : 350,
			//layout : 'accordion',  
			collapsible : true
		});
		var rightPanel = Ext.create('Ext.tab.Panel', {
			id : 'rightPanel',
			region : 'center',
			layout : 'fit',
			items : [ {
				title : '首页',
				closable : true
			} ],
			plugins : Ext.create('Ext.ux.TabCloseMenu'),
			listeners : {
				remove : function(tp, c) {
					c.hide();
				}
			}
		});
		var buildTree = function(json) {
			return Ext.create('Ext.tree.Panel', {
				rootVisible : false,
				border : false,
				store : Ext.create('Ext.data.TreeStore', {
					root : {
						expanded : true,
						children : json.children
					}
				}),
				listeners : {
					'itemclick' : function(view, record, item, index, e) {
						var id = record.get('id');
						var text = record.get('text');
						var leaf = record.get('leaf');
						var url = '..' + getRootPath()
								+ record.get('hrefTarget');
						// alert(id);  
						if (leaf) {
							addTab(id, text, url);
						}
					},
					scope : this
				}
			});
		};
		Ext.Ajax.request({
			url : '../resourceController/getMenu.action',
			success : function(response) {
				var r = response.responseText;
				//r = r.replaceAll("checked", "");
				var json = Ext.JSON.decode(r);
				// alert(json);  
				Ext.each(json, function(el) {
					var panel = Ext.create('Ext.Panel', {
						id : el.id,
						title : 'xxxxxxxxxxxxxxxxxx',
						layout : 'fit'
					});
					panel.add(buildTree(el));
					leftPanel.add(panel);
				});
			},
			failure : function(request) {
				Ext.MessageBox.show({
					title : '提示',
					msg : "操作失败",
					buttons : Ext.MessageBox.OK,
					icon : Ext.MessageBox.ERROR
				});
			},
			method : 'post'
		});

		var north = Ext.create('Ext.panel.Panel', {
			region : 'north',
			contentEl : 'x-north'
		});
		var south = Ext.create('Ext.panel.Panel', {
			region : 'south',
			contentEl : 'south-x',
			height : '5%'
		});
		/*new Ext.Viewport({ 
		    id : 'main', 
		    layout : 'border', 
		    items : [ north, south, leftPanel, rightPanel ] 
		});*/
		Ext.create('Ext.Viewport', {
			layout : {
				type : 'border',
				padding : 5
			},
			defaults : {
				split : true
			},
			items : [ north, leftPanel ]
		});

		leftPanel.doLayout();
		//修改密码form  
		var modifyForm = Ext.widget('form', {
			frame : false,
			bodyPadding : 5,
			fieldDefaults : {
				lablelWidth : 75,
				msgTarget : 'side'
			},
			defaults : {
				anchor : '100%'
			},
			items : [ {
				xtype : 'textfield',
				fieldLabel : '登录用户名',
				name : 'usernames',
				value : vUserLoginName,
				readOnly : true
			}, {
				xtype : 'textfield',
				fieldLabel : '原始密码',
				name : 'oldPassword',
				inputType : 'password',
				maxLength : 29,
				display : false,
				flex : 1,
				allowBlank : false
			}, {
				xtype : 'textfield',
				fieldLabel : '新密码',
				name : 'newPassword1',
				inputType : 'password',
				maxLength : 29,
				display : false,
				flex : 1,
				allowBlank : false
			}, {
				xtype : 'textfield',
				fieldLabel : '确认新密码',
				name : 'newPassword2',
				id : 'password2',
				inputType : 'password',
				display : false,
				flex : 1,
				allowBlank : false,
				maxLength : 29,
				minLength : 8,
				validator : function(value) {
					var passwrod = this.previousSibling('[name=newPassword1]');
					return (value == passwrod.getValue()) ? true : '两次输入密码不相同'
				}
			}, {
				xtype : 'textfield',
				name : 'id',
				itemId : 'id',
				value : vUserCode,
				hidden : true,
				hideLabel : true
			} ],
			buttons : [
					{
						text : '保存',
						handler : function() {
							var cform = modifyForm.getForm();
							if (cform.isValid()) {
								var request = Ext.Ajax.request({
									method : 'post',
									url : getRootPath()
											+ '/user.do?method=updatePwd',
									params : {
										cUserInfo : Ext.encode(cform
												.getValues())
									},
									success : function(response) {
										var data = Ext.JSON
												.decode(response.responseText);
										if (data.res) {
											modifyPwdWin.close();
										}
										Ext.Msg.alert("操作结果信息", data.msg);
									},
									failure : function(response) {
										Ext.Msg.alert("错误", "无法访问后台,请稍后重试");
									}
								});//end var request  
							}
						}
					}, {
						text : '重置',
						handler : function() {
							modifyForm.getForm().reset();
						}
					}, {
						text : '关闭',
						handler : function() {
							modifyPwdWin.close();
						}
					} ]
		});

		//用户密码修改  
		Ext.get('modifyPwd').on('click', function(e) {
			modifyPwdWin = Ext.widget('window', {
				title : '密码修改',
				closeAction : 'hide',
				width : 400,
				height : 300,
				minHeight : 300,
				layout : 'fit',
				resizable : true,
				modal : true,
				items : [ modifyForm ],
				listeners : {
					hide : function() {
						modifyForm.getForm().reset();
					}
				}
			});
			modifyPwdWin.show();

			/*alert(vUserLoginName); 
			alert(vUserCode);*/
		});
	});
</script>
</head>
<body>
</body>
</html>